<?php 

class AdministrationInscritModel extends CI_Model {

	function getListeInscrit()
	{        
		$this->db->select('iduser,nom,prenom,email,typeuser,enAttenteValidation');
		$this->db->from('user_view');  
		return $this->db->get();
	}

	function supprimerInscrit($id)
	{
		 $this->db->where('iduser', $id);
		 $this->db->update('user' , array('supprimer' => 1));
		 return true;
	}
	
	
    function getDetailInscrit()
    {         
        $this->db->select('iduser,nom,prenom,ville,email,typeuser,enAttenteValidation');
        $this->db->from('user');  
        $this->db->where('iduser',$_GET["IdUser"]);  
        $query = $this->db->get();  
        $rowcount = $query->num_rows();
        $query = $query->result();
        
        if($rowcount==1)
        {
            $this->nom=$query[0]->nom;
            $this->prenom=$query[0]->prenom;
            $this->ville=$query[0]->ville;
            $this->mail=$query[0]->email;
            $this->typeuser=$query[0]->typeuser;
            $this->enAttenteValidation=$query[0]->enAttenteValidation;
        }
        
        $this->db->select('idmodule,module.nom,idenseignant');
        $this->db->from('module');            
        $this->listeModuleAll = $this->db->get();  
        
        $this->db->select('idmodule');
        $this->db->from('module');
        $this->db->join('module_has_user',"module.idmodule=module_has_user.module_idmodule");
        $this->db->join('user',"user.iduser=module_has_user.user_iduser");
        $this->db->where('iduser',$_GET["IdUser"]);         
        $this->listeModuleUser = $this->db->get()->result();  
    }
    
    function saveDetailInscrit()
    {         
        /** mise � jour du user **/ 
        $this->db->select('iduser,typeuser');
        $this->db->from('user');  
        $this->db->where('iduser',$_POST["idUser"]); 
        $userEnCours = $this->db->get()->result();
		
	if ($userEnCours[0]->typeuser == 3) {
            $typeuser = 3;
        } else {
            if(isset($_POST["enseignant"]))
            {
                $typeuser = 2;                      
            }
	    else
	    {
                $typeuser = 1;
            }
	}
        
        $data = array(
			'nom' => $_POST["nom"],
			'prenom' => $_POST["prenom"],
			'ville' => $_POST["ville"],
			'email' => $_POST["email"] ,
			'typeuser' => $typeuser
		);
    
		$this->db->where('iduser', $_POST["idUser"]);
		$this->db->update('user', $data);        

		/** mise a jour liste module **/
		$this->db->where('user_iduser', $_POST["idUser"]);
		$this->db->delete('module_has_user'); 

		if(isset($_POST["user_module"]))
		{
			foreach ($_POST["user_module"] as $value)
			{
				$dataModule = array(
					'module_idmodule' => $value ,
					'user_iduser' => $_POST["idUser"] 
				);
				$this->db->insert('module_has_user', $dataModule); 
                                
                                if(isset($_POST["enseignant"]))
                                {
                                    $dataEnseign = array(
                                            'idenseignant' => $_POST["idUser"]
                                    );
                                    $this->db->where('idmodule', $value);
                                    $this->db->update('module', $dataEnseign);
                                }
                                
                                
			}
		}
		
    }
   
    function confirmerInscrit()
    {
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'eve.emaige@gmail.com';
        $config['smtp_pass']    = '2Bse6Vu3ZdYTwu4u';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'html'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not     
        
        
        $this->load->library('email');
        $this->db->select('iduser,email,password');
        $this->db->from('user');  
        $this->db->where('iduser',$_POST["IdUser"]);  
        $query = $this->db->get()->result();          
               
        $this->email->initialize($config);
        $this->email->from('eve.emaige@gmail.com', 'Administrateur Eve');
        $this->email->to($query[0]->email);
        
        $this->email->subject('Confirmation inscription Eve');
        
         //TODO : body email et decommenter send
        $this->email->message('Bienvenue sur le site eve<br/><br/>Afin de poouvoir vous connecter voici les &eacute;l&eacute;ments:<br/> - adresse du site : http://eve.mydashboard.fr/ <br/> - votre utilisateur : '.$query[0]->email.'<br/> - votre mot de passe : '.$query[0]->password.'<br/><br/>Cordialement,<br/>Adminstrateur Eve');
        
       
        //$this->email->send();
         if ( ! $this->email->send()) {
           $this->db->where('iduser', $_POST["idUser"]);
            $this->db->update('user', array('enAttente' => 0));  
            return true;  
        } 
        else {
           return false;   
        } 
    }
}
